package main

import (
	"flag"
	"fmt"

	"monitor/user/api/internal/config"
	"monitor/user/api/internal/handler"
	"monitor/user/api/internal/svc"

	"github.com/jasonlvhit/gocron"
	"github.com/tal-tech/go-zero/core/conf"
	"github.com/tal-tech/go-zero/rest"
)

var configFile = flag.String("f", "etc/user-api.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	handler.RegisterHandlers(server, ctx)

	gocron.Every(1).Day().At("23:00").Do(statEveryDay, ctx)
	fmt.Println("定时后台任务开启")
	gocron.Start()

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()

}

/**
每日统计 每天晚上11点来一次统计
*/
func statEveryDay(ctx *svc.ServiceContext) {
	fmt.Println("statEveryDay ENTER")
	err := ctx.StatModel.StatEveryDay()
	if err != nil {
		fmt.Println("statEveryDay Error," + err.Error())
	}
}
